/*
 * Angry Monsters, Windows Mobile 7 XNA Game FW demo
 * Tero Paananen, tepaanan@gmail.com
 * 2011 Finland
*/

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Xna.Framework.Media;

// http://box2dxna.codeplex.com
using Box2D.XNA;


namespace WP7Box2d
{
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        // Box2D
        private BoxHandler m_boxHandler = null;

        LevelHandler m_levelHandler = null;

        LevelPreview m_levelPreview = null;

        GameEngine m_gameEngine = null;

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";

            graphics.IsFullScreen = true;

            this.IsFixedTimeStep = true;

            // Frame rate is 30 fps by default for Windows Phone.
            //TargetElapsedTime = TimeSpan.FromTicks(333333);

            // Frame rate is 30 fps by default for Windows Phone but it that's 
            // too slow for Box2D so the target frame rate is set to 60 fps.
            TargetElapsedTime = TimeSpan.FromTicks(166666);

        }

        protected override void Initialize()
        {
            // Box2D handler class
            m_boxHandler = new BoxHandler(this.Content);

            // Level handler
            m_levelHandler = new LevelHandler(this.Content, ref m_boxHandler);
            m_levelHandler.CreateLevelStorage(true);

            m_levelPreview = new LevelPreview(this.Content, ref m_boxHandler);
            m_levelPreview.CreateLevelStorage(false);

            // Game engine
            m_gameEngine = new GameEngine(this, ref m_boxHandler, ref m_levelHandler, ref m_levelPreview);

            // Create Box2D World. Screen is 800 x 480
            m_boxHandler.CreateWorld(new Vector2(GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height));

            // Seems that is best to keep that last
            base.Initialize();
        }

        protected override void LoadContent()
        {
            spriteBatch = new SpriteBatch(GraphicsDevice);

            // Load textures for Box2D
            m_boxHandler.LoadSplashScreenContent(this.Content);
            m_boxHandler.CreateCatapult();

            // Game engine contents
            m_gameEngine.LoadContent(this.Content);

            // Rest textures are loaded before 
        }

        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
            this.Content.Unload();
        }

        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
            {
                if (m_gameEngine.m_gameState == GameEngine.GameState.EMenuScreenState)
                {
                    // Exit
                    this.Exit();
                }
                else
                {
                    // Back
                    m_gameEngine.GameStateChanged(GameEngine.GameState.EMenuScreenState);
                }
                
            }

            // Update game engine
            m_gameEngine.Update(gameTime);

            // Update Game
            base.Update(gameTime);
        }

        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.White);
         
            spriteBatch.Begin();
                        
            m_gameEngine.Draw(spriteBatch, gameTime);

            spriteBatch.End();
            base.Draw(gameTime);
        }
    }
}
